<?php

namespace Gnway\Ticket\Rest\Response;

use Gnway\Ticket\Rest\Contracts\HttpResponseInterface;
use Gnway\Ticket\Rest\Util\ResponseUtils;

/**
 * Class ResponseHandler
 *
 * @deprecated
 * @see ResponseUtils::send()
 * @see ResponseUtils::sendHeaders()
 * @package Gnway\Ticket\Rest\Response
 */
class ResponseHandler
{

  /**
   * Send Response
   *
   * @param HttpResponseInterface|null $response
   */
  public static function sendResponse(?HttpResponseInterface $response): void
  {
    if ($response === NULL) {
      return;
    }

    self::sendResponseHeaders($response->getHttpResponseCode(), $response->getHttpResponseHeaders());

    echo $response->getValue();

    function_exists('fastcgi_finish_request') && fastcgi_finish_request();
  }

  /**
   * Send Http headers
   *
   * @param int   $code
   * @param array $headers
   */
  private static function sendResponseHeaders(int $code, array $headers): void
  {
    if (headers_sent()) {
      return;
    }

    foreach ($headers as $header) {
      header($header, TRUE, $code);
    }
  }
}